﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using DiscoveryLogic.Common.Utilities;

namespace DiscoveryLogic.DataViz.NetworkDiagram.LinkerControls
{
    /// <summary>
    /// 
    /// </summary>
    public class NetworkJoin
    {
        #region props
        private string _FromNetworkName;
        /// <summary></summary>
        public string FromNetworkName
        {
            get
            {
                return this._FromNetworkName;
            }
            set
            {
                this._FromNetworkName = value;
            }
        }

        private string _ToNetworkName;
        /// <summary></summary>
        public string ToNetworkName
        {
            get
            {
                return this._ToNetworkName;
            }
            set
            {
                this._ToNetworkName = value;
            }
        }

        private string _FromObjMemberName;
        /// <summary></summary>
        public string FromObjMemberName
        {
            get
            {
                return this._FromObjMemberName;
            }
            set
            {
                this._FromObjMemberName = value;
            }
        }

        private string _ToObjMemberName;
        /// <summary></summary>
        public string ToObjMemberName
        {
            get
            {
                return this._ToObjMemberName;
            }
            set
            {
                this._ToObjMemberName = value;
            }
        }

        private string _JoinCalcTypeName;
        /// <summary></summary>
        public string JoinCalcTypeName
        {
            get
            {
                return this._JoinCalcTypeName;
            }
            set
            {
                this._JoinCalcTypeName = value;
            }
        }

        private NetworkLinkBehavior _Behavior;
        /// <summary></summary>
        public NetworkLinkBehavior Behavior
        {
            get
            {
                return this._Behavior;
            }
            set
            {
                this._Behavior = value;
            }
        }

        private double _AttractingForce = 1.0;
        /// <summary></summary>
        public double AttractingForce
        {
            get
            {
                return this._AttractingForce;
            }
            set
            {
                this._AttractingForce = value;
            }
        }

        private double _RepellingForce = 2.0;
        /// <summary></summary>
        public double RepellingForce
        {
            get
            {
                return this._RepellingForce;
            }
            set
            {
                this._RepellingForce = value;
            }
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="joinNode"></param>
        public void Save(ref XmlDocument xDoc, XmlNode joinNode)
        {
            XmlDataUtil.UpdateAttribute(ref xDoc, joinNode, "FromNetwork",this.FromNetworkName);
            XmlDataUtil.UpdateAttribute(ref xDoc, joinNode, "ToNetwork",this.ToNetworkName);
            XmlDataUtil.UpdateAttribute(ref xDoc, joinNode, "FromMember",this.FromObjMemberName);
            XmlDataUtil.UpdateAttribute(ref xDoc, joinNode, "ToMember",this.ToObjMemberName);
            XmlDataUtil.UpdateAttribute(ref xDoc, joinNode, "JoinCalcType",this.JoinCalcTypeName);
            XmlDataUtil.UpdateAttribute(ref xDoc, joinNode, "JoinBehavior",this.Behavior.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, joinNode, "AttractingForce", this.AttractingForce.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, joinNode, "RepellingForce",this.RepellingForce.ToString());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="joinNode"></param>
        /// <returns></returns>
        public static NetworkJoin Read(XmlNode joinNode)
        {
            NetworkJoin join = new NetworkJoin();
            join.FromNetworkName = XmlDataUtil.GetAttributeValue(joinNode, "FromNetwork", "");
            join.ToNetworkName = XmlDataUtil.GetAttributeValue(joinNode, "ToNetwork", "");
            join.FromObjMemberName = XmlDataUtil.GetAttributeValue(joinNode, "FromMember", "");
            join.ToObjMemberName = XmlDataUtil.GetAttributeValue(joinNode, "ToMember", "");
            join.JoinCalcTypeName = XmlDataUtil.GetAttributeValue(joinNode, "JoinCalcType", "");
            join.Behavior =
                (NetworkLinkBehavior)
                Enum.Parse(
                    typeof (NetworkLinkBehavior),
                    XmlDataUtil.GetAttributeValue(joinNode, "JoinBehavior", NetworkLinkBehavior.LeftJoin.ToString()));
            join.AttractingForce = double.Parse(XmlDataUtil.GetAttributeValue(joinNode, "AttractingForce", "1.0"));
            join.RepellingForce = double.Parse(XmlDataUtil.GetAttributeValue(joinNode, "RepellingForce", "2.0"));
            return join;
        }
    }
}
